'use strict';

const Service = require('egg').Service;

class Articles extends Service {
	// 列表
	async list(data) {
		return this.ctx.model.Article.findAndCountAll({
			where:data.where,
			limit:data.limit,
			offset:data.offset,
			order: [
				['created_at', 'desc'],
				['id', 'desc']
			],
		});
	}
	//新增
	async save(data) {
		const newArticle = await this.ctx.model.Article.create(data);
		console.log(newArticle);
		return newArticle;
	}
	//查询
	async findOne(data) {
		const article = await this.ctx.model.Article.findOne({
			where: data,
			attributes:{
				exclude:['password']
			}
		})
		return article;
	}

	//修改
	async update(data) {
		const article = await this.ctx.model.Article.update(
			data,
			{
				where:{
					id:data.id
				},
			}
		)
		return article;
	}
}

module.exports = Articles;